package com.android.presenter;

import android.content.Context;
import android.os.Handler;
import android.util.Log;

import com.android.app.CommonType;
import com.android.app.constant.UrlConstants;
import com.android.model.entity.res.QuoteKLineBean;
import com.android.presenter.contract.HqKLinesContract;
import com.android.util.JsonUtil;
import com.android.util.LogUtils;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.mp.chart.charts.KLineCombinedChart;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;

public class HqKLinePresenter implements HqKLinesContract.Presenter {

    private HqKLinesContract.View view;
    private String kLinesTime = null;
    private String time;
    private String URL = "";
    private String symbol = "";
    private String historicalQuoteInterval = "";
    private String adjustmentMethod = "";
    private long endDate;
    private int timeNumber;
    private String date = "";
    private Date curDate = new Date(System.currentTimeMillis());//获取当前时间
    private List<QuoteKLineBean.DataBean> dataList;
    private boolean kLineRefreshStatus = true;
    private double oldDataClose = 0;
    private double oldVolume = 0;
    private int firstLoadHistory = 1;//收盘情况下，刚进入K线View时第一次加载历史数据
    private String quoteObject;
    private Context mContext;

    public HqKLinePresenter(Context context, String code, String time, String beginDay, HqKLinesContract.View view, KLineCombinedChart mKLineCombinedChart) {
        this.view = view;
        this.time = time;
        this.mContext = context;
//        EventBus.getDefault().register(this);
    }


    public void setKLinesTime(String kLinesTime) {
        if (kLinesTime == null || !kLinesTime.equals(this.kLinesTime)) {
            this.kLinesTime = kLinesTime;
            getLine(false);
        }
    }

    @Override
    public void getLine(boolean forceRefresh) {
//CommonType.TIME_TYPE_MIN, CommonType.TIME_TYPE_ONE_MIN, CommonType.TIME_TYPE_ONE_HOUR, CommonType.TIME_TYPE_DAY
        switch (time) {
            case CommonType.TIME_TYPE_MIN:
//                URL = CommonURL.QUOTE_NEW_FACADE + CommonURL.QUOTE_HISTORICAL_K_LINE;
                URL = "";
                historicalQuoteInterval = "DAY";
                adjustmentMethod = "ALL";
                endDate = curDate.getTime();
                timeNumber = 90 * 2;
                date = historicalQuoteInterval;

                OkGo.get(UrlConstants.HOST_HTTPS + UrlConstants.MARKET_SUMMARIZED)
                        .execute(new StringCallback() {
                            @Override
                            public void onSuccess(String s, Call call, Response response) {
                                QuoteKLineBean marketDataResult = JsonUtil.parseJsonToBean(s, QuoteKLineBean.class);
                                if (marketDataResult != null) {
                                    dataList = new ArrayList<>();
                                    dataList.addAll(marketDataResult.getData());
                                    //延迟1ms画图，不延迟的话图表不能移动到最后
                                    new Handler().postDelayed(new Runnable() {
                                        public void run() {
                                            view.updateLine(dataList, 0);
                                        }
                                    }, 1);
                                }
                            }

                            @Override
                            public void onError(Call call, Response response, Exception e) {
                                super.onError(call, response, e);
                                LogUtils.e(e.getMessage());
                            }
                        });


//                try {
//                    //判断缓存中有没有K线的数据，有的话就去加载缓存中的数据，并且画图，没有就去请求
////                    String spKLineDayData = GalaxyMainActivity.userPreferences.getString(CommonType.QUOTE_KLINE_DATA_DAY, null);
//                    String spKLineDayData = LocalSp.getInstance(mContext).getDay();
//                    if (spKLineDayData != null) {
//                        QuoteKLineBean quoteKLineBean = JsonUtil.parseJsonToBean(spKLineDayData, QuoteKLineBean.class);
//                        if (quoteKLineBean != null) {
//                            String status = quoteKLineBean.getStatus();
//                            if (!StringUtils.isEmpty(status) && (status.equals(CommonType.STATUS_TYPE_QUOTE_SUCCESS) || status.equals(CommonType.STATUS_TYPE_SUCCESS))) {
//                                dataList = new ArrayList<>();
//                                dataList.addAll(quoteKLineBean.getData());
//                                oldVolume = dataList.get(0).getVolume();
//                                //延迟1ms画图，不延迟的话图表不能移动到最后
//                                new Handler().postDelayed(new Runnable() {
//                                    public void run() {
//                                        view.updateLine(dataList, 0);
//                                    }
//                                }, 1);
//
//                            }
//                        }
//
//                    } else {
//                        requestFirstData(URL, symbol, historicalQuoteInterval, adjustmentMethod, endDate, timeNumber, date);
//                    }
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
                break;
            case CommonType.TIME_TYPE_ONE_MIN:

                URL = "";
                historicalQuoteInterval = "WEEK";
                adjustmentMethod = "ALL";
                endDate = curDate.getTime();
                timeNumber = 90 * 2;
                date = historicalQuoteInterval;

                OkGo.get("http://test-api.dev.bgaex.com:8080/quote-marketdata/quote/historical.timeRange")
                        .execute(new StringCallback() {
                            @Override
                            public void onSuccess(String s, Call call, Response response) {
                                QuoteKLineBean marketDataResult = JsonUtil.parseJsonToBean(s, QuoteKLineBean.class);
                                if (marketDataResult != null) {
                                    dataList = new ArrayList<>();
                                    dataList.addAll(marketDataResult.getData());
                                    //延迟1ms画图，不延迟的话图表不能移动到最后
                                    new Handler().postDelayed(new Runnable() {
                                        public void run() {
                                            view.updateLine(dataList, 0);
                                        }
                                    }, 1);
                                }
                            }

                            @Override
                            public void onError(Call call, Response response, Exception e) {
                                super.onError(call, response, e);
                            }
                        });

//                try {
//                    //判断缓存中有没有K线的数据，有的话就去加载缓存中的数据，并且画图，没有就去请求
////                    String spKLineWeekData = GalaxyMainActivity.userPreferences.getString(CommonType.QUOTE_KLINE_DATA_WEEK, null);
//                    String spKLineWeekData = LocalSp.getInstance(mContext).getWeek();
//                    if (spKLineWeekData != null) {
//                        QuoteKLineBean quoteKLineBean = JsonUtil.parseJsonToBean(spKLineWeekData, QuoteKLineBean.class);
//                        if (quoteKLineBean != null) {
//                            String status = quoteKLineBean.getStatus();
//                            if (!StringUtils.isEmpty(status) && (status.equals(CommonType.STATUS_TYPE_QUOTE_SUCCESS) || status.equals(CommonType.STATUS_TYPE_SUCCESS))) {
//                                dataList = new ArrayList<>();
//                                dataList.addAll(quoteKLineBean.getData());
//                                oldVolume = dataList.get(0).getVolume();
//                                //延迟100ms画图，不延迟的话图表不能移动到最后
//                                new Handler().postDelayed(new Runnable() {
//                                    public void run() {
//                                        view.updateLine(dataList, 0);
//                                    }
//                                }, 100);
//                            }
//                        }
//                    } else {
//                        requestFirstData(URL, symbol, historicalQuoteInterval, adjustmentMethod, endDate, timeNumber, date);
//                    }
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
                break;
            case CommonType.TIME_TYPE_ONE_HOUR:

                URL = "";
                historicalQuoteInterval = "MONTH";
                adjustmentMethod = "ALL";
                endDate = curDate.getTime();
                timeNumber = 90 * 2;
                date = historicalQuoteInterval;

                OkGo.get("http://test-api.dev.bgaex.com:8080/quote-marketdata/quote/historical.timeRange")
                        .execute(new StringCallback() {
                            @Override
                            public void onSuccess(String s, Call call, Response response) {
                                QuoteKLineBean marketDataResult = JsonUtil.parseJsonToBean(s, QuoteKLineBean.class);
                                if (marketDataResult != null) {
                                    dataList = new ArrayList<>();
                                    dataList.addAll(marketDataResult.getData());
                                    //延迟1ms画图，不延迟的话图表不能移动到最后
                                    new Handler().postDelayed(new Runnable() {
                                        public void run() {
                                            view.updateLine(dataList, 0);
                                        }
                                    }, 1);
                                }
                            }

                            @Override
                            public void onError(Call call, Response response, Exception e) {
                                super.onError(call, response, e);
                            }
                        });

//                try {
//                    //判断缓存中有没有K线的数据，有的话就去加载缓存中的数据，并且画图，没有就去请求
////                    String spKLineMonthData = GalaxyMainActivity.userPreferences.getString(CommonType.QUOTE_KLINE_DATA_MONTH, null);
//                    String spKLineMonthData = LocalSp.getInstance(mContext).getMonth();
//                    if (spKLineMonthData != null) {
//                        QuoteKLineBean quoteKLineBean = JsonUtil.parseJsonToBean(spKLineMonthData, QuoteKLineBean.class);
//                        if (quoteKLineBean != null) {
//                            String status = quoteKLineBean.getStatus();
//                            if (!StringUtils.isEmpty(status) && (status.equals(CommonType.STATUS_TYPE_QUOTE_SUCCESS) || status.equals(CommonType.STATUS_TYPE_SUCCESS))) {
//                                dataList = new ArrayList<>();
//                                dataList.addAll(quoteKLineBean.getData());
//                                oldVolume = dataList.get(0).getVolume();
//                                //延迟100ms画图，不延迟的话图表不能移动到最后
//                                new Handler().postDelayed(new Runnable() {
//                                    public void run() {
//                                        view.updateLine(dataList, 0);
//                                    }
//                                }, 100);
//                            }
//                        }
//                    } else {
//                        requestFirstData(URL, symbol, historicalQuoteInterval, adjustmentMethod, endDate, timeNumber, date);
//                    }
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
                break;

            case CommonType.TIME_TYPE_DAY:

                URL = "";
                historicalQuoteInterval = "MONTH";
                adjustmentMethod = "ALL";
                endDate = curDate.getTime();
                timeNumber = 90 * 2;
                date = historicalQuoteInterval;

                OkGo.get("http://test-api.dev.bgaex.com:8080/quote-marketdata/quote/historical.timeRange")
                        .execute(new StringCallback() {
                            @Override
                            public void onSuccess(String s, Call call, Response response) {
                                QuoteKLineBean marketDataResult = JsonUtil.parseJsonToBean(s, QuoteKLineBean.class);
                                if (marketDataResult != null) {
                                    dataList = new ArrayList<>();
                                    dataList.addAll(marketDataResult.getData());
                                    //延迟1ms画图，不延迟的话图表不能移动到最后
                                    new Handler().postDelayed(new Runnable() {
                                        public void run() {
                                            view.updateLine(dataList, 0);
                                        }
                                    }, 1);
                                }
                            }

                            @Override
                            public void onError(Call call, Response response, Exception e) {
                                super.onError(call, response, e);
                            }
                        });

//                try {
//                    //判断缓存中有没有K线的数据，有的话就去加载缓存中的数据，并且画图，没有就去请求
////                    String spKLineMonthData = GalaxyMainActivity.userPreferences.getString(CommonType.QUOTE_KLINE_DATA_MONTH, null);
//                    String spKLineMonthData = LocalSp.getInstance(mContext).getMonth();
//                    if (spKLineMonthData != null) {
//                        QuoteKLineBean quoteKLineBean = JsonUtil.parseJsonToBean(spKLineMonthData, QuoteKLineBean.class);
//                        if (quoteKLineBean != null) {
//                            String status = quoteKLineBean.getStatus();
//                            if (!StringUtils.isEmpty(status) && (status.equals(CommonType.STATUS_TYPE_QUOTE_SUCCESS) || status.equals(CommonType.STATUS_TYPE_SUCCESS))) {
//                                dataList = new ArrayList<>();
//                                dataList.addAll(quoteKLineBean.getData());
//                                oldVolume = dataList.get(0).getVolume();
//                                //延迟100ms画图，不延迟的话图表不能移动到最后
//                                new Handler().postDelayed(new Runnable() {
//                                    public void run() {
//                                        view.updateLine(dataList, 0);
//                                    }
//                                }, 100);
//                            }
//                        }
//                    } else {
//                        requestFirstData(URL, symbol, historicalQuoteInterval, adjustmentMethod, endDate, timeNumber, date);
//                    }
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
                break;
        }

    }


//    @Subscribe
//    public void onEventMainThread(BusUtils.KLineRefreshStatus refreshStatus) {
//        kLineRefreshStatus = refreshStatus.canRefresh;
//    }

//    @Subscribe
//    public void onEventMainThread(BusUtils.BusSnapData busData) {
//
//        try {
//            final QuoteStockInfoSnapData quoteStockInfoSnapData = busData.getData();//Snap的数据
//
//            if (!TextUtils.isEmpty(QuoteStockInfoActivity.stockCode)
//                    && quoteStockInfoSnapData.getData().getCode().equals(QuoteStockInfoActivity.stockCode)) {
//
//                if (kLineRefreshStatus) {
//
//                    AppBaseNetWork.getInstance().isStockOpeningTime(new AppBaseNetWork.StateCallback() {
//                        @Override
//                        public void isSuccess(boolean isSuccess) {
//
//                            //快照数据转K线数据
//                            if (dataList != null && dataList.size() > 0) {
//                                QuoteKLineBean.DataBean oldLastKLineDataBean = dataList.get(0);//老的K线的最后一条数据
//                                QuoteKLineBean.DataBean newLastKLineDataBean = new QuoteKLineBean.DataBean();//Snap转化的最后一条数据
//
//
//                                long oldCloseData = oldLastKLineDataBean.getCloseDate();
//                                DateTime oldDateTime = new DateTime(oldCloseData);
//                                int oldMonth = oldDateTime.getMonthOfYear();
//                                int oldWeek = oldDateTime.getWeekOfWeekyear();
//                                int oldDay = oldDateTime.getDayOfWeek();
//
//                                //获取snap的时间戳
//                                String dealTime = quoteStockInfoSnapData.getData().getDealtime();
//                                if (dealTime.contains("-0400")) {
//                                    dealTime = dealTime.replace("-0400", "+0800");
//                                } else if (dealTime.contains("-0500")) {
//                                    dealTime = dealTime.replace("-0500", "+0800");
//                                }
//                                Date date = new Date(dealTime);
//                                long snapTimeStamp = date.getTime();
//
//
//                                DateTime snapDateTime = new DateTime(snapTimeStamp);
//                                int snapMonth = snapDateTime.getMonthOfYear();
//                                int snapWeek = snapDateTime.getWeekOfWeekyear();
//                                int snapDay = snapDateTime.getDayOfWeek();
//
//                                if (isSuccess) {
//                                    //盘中
//                                    //判断close data与snap data是否相等
//                                    //1，是，return
//                                    //2，否，判断日是否相等
//                                    //      1⃣️，是，修改最后一条
//                                    //      2⃣️，否，switch
//                                    //          A，日K，直接添加
//                                    //          B，周K，判断week是否同周
//                                    //                    a，是，修改最后一条
//                                    //                    b，否，直接添加
//                                    //          C，月K一样同周K
//
//                                    //判断close data与snap data是否相等
//                                    if (oldCloseData != snapTimeStamp) {
//                                        //2，否，判断日是否相等
//                                        if (oldDay == snapDay) {
//                                            oldDataClose = oldLastKLineDataBean.getPreviousClose();
//                                            //      1⃣️，是，修改最后一条
//                                            replaceAllSnapData(quoteStockInfoSnapData, oldLastKLineDataBean, newLastKLineDataBean, snapTimeStamp, oldDataClose);
//
//                                        } else {
//
//                                            //      2⃣️，否
//                                            switch (time) {
//                                                case "day":
//                                                    //          A，日K，直接添加
//                                                    addAllSnapData(quoteStockInfoSnapData, oldLastKLineDataBean, newLastKLineDataBean, snapTimeStamp);
//
//                                                    break;
//                                                case "week":
//
//                                                    //          B，周K，判断week是否同周
//                                                    //                    a, 是，修改最后一条
//                                                    //                    b, 否，直接添加
//                                                    if (oldWeek == snapWeek) {
//                                                        oldDataClose = dataList.get(0).getPreviousClose();
//                                                        //a，是，修改最后一条
//                                                        replaceAllSnapData(quoteStockInfoSnapData, oldLastKLineDataBean, newLastKLineDataBean, snapTimeStamp, oldDataClose);
//
//                                                    } else {
//                                                        oldDataClose = quoteStockInfoSnapData.getData().getPrev();
//                                                        //b，否，直接添加
//                                                        addAllSnapData(quoteStockInfoSnapData, oldLastKLineDataBean, newLastKLineDataBean, snapTimeStamp);
//
//                                                    }
//                                                    break;
//                                                case "month":
//
//                                                    //          C，月K，判断month是否同月
//                                                    //                    a，是，修改最后一条
//                                                    //                    b，否，直接添加
//
//                                                    if (oldMonth == snapMonth) {
//                                                        oldDataClose = dataList.get(0).getPreviousClose();
//                                                        //a,是，修改最后一条
//                                                        replaceAllSnapData(quoteStockInfoSnapData, oldLastKLineDataBean, newLastKLineDataBean, snapTimeStamp, oldDataClose);
//
//                                                    } else {
//                                                        oldDataClose = quoteStockInfoSnapData.getData().getPrev();
//                                                        //b，否，直接添加
//                                                        addAllSnapData(quoteStockInfoSnapData, oldLastKLineDataBean, newLastKLineDataBean, snapTimeStamp);
//
//                                                    }
//                                                    break;
//                                            }
//
//                                        }
//                                    } else {
//                                        //不操作
////                                        view.updateLine(dataList, 0);
//
//                                    }
//
//
//                                } else {
//                                    //盘后
//                                    //判断历史是否有当天
//                                    //1，否，判断日是否相等
//                                    //      1⃣️，是，修改最后一条
//                                    //      2⃣️，否，switch
//                                    //          A，日K，直接添加
//                                    //          B，周K，判断week是否同周
//                                    //                    a，是，修改最后一条
//                                    //                    b，否，直接添加
//                                    //          C，月K一样同周K
//                                    //2，是，return
//                                    if (oldDay != snapDay) {
////                                        //1，否，快照合到data list
////                                        addAllSnapData(quoteStockInfoSnapData, oldLastKLineDataBean, newLastKLineDataBean, snapTimeStamp);
//
//                                        //      2⃣️，否
//                                        switch (time) {
//                                            case "day":
//                                                //          A，日K，直接添加
//                                                addAllSnapData(quoteStockInfoSnapData, oldLastKLineDataBean, newLastKLineDataBean, snapTimeStamp);
//
//                                                break;
//                                            case "week":
//
//                                                //          B，周K，判断week是否同周
//                                                //                    a, 是，修改最后一条
//                                                //                    b, 否，直接添加
//                                                if (oldWeek == snapWeek) {
//                                                    oldDataClose = dataList.get(0).getPreviousClose();
//                                                    //a，是，修改最后一条
//                                                    replaceAllSnapData(quoteStockInfoSnapData, oldLastKLineDataBean, newLastKLineDataBean, snapTimeStamp, oldDataClose);
//
//                                                } else {
//                                                    oldDataClose = quoteStockInfoSnapData.getData().getPrev();
//                                                    //b，否，直接添加
//                                                    addAllSnapData(quoteStockInfoSnapData, oldLastKLineDataBean, newLastKLineDataBean, snapTimeStamp);
//
//                                                }
//                                                break;
//                                            case "month":
//
//                                                //          C，月K，判断month是否同月
//                                                //                    a，是，修改最后一条
//                                                //                    b，否，直接添加
//
//                                                if (oldMonth == snapMonth) {
//                                                    oldDataClose = dataList.get(0).getPreviousClose();
//                                                    //a,是，修改最后一条
//                                                    replaceAllSnapData(quoteStockInfoSnapData, oldLastKLineDataBean, newLastKLineDataBean, snapTimeStamp, oldDataClose);
//
//                                                } else {
//                                                    oldDataClose = quoteStockInfoSnapData.getData().getPrev();
//                                                    //b，否，直接添加
//                                                    addAllSnapData(quoteStockInfoSnapData, oldLastKLineDataBean, newLastKLineDataBean, snapTimeStamp);
//
//                                                }
//                                                break;
//                                        }
//
//
//                                    } else {
//                                        //2，是，return
//                                        //只有第一次进来的时候才去画图，以保证分页加载时这里不刷新视图，解决分页加载滑动到最后的bug
//                                        if (firstLoadHistory == 1) {
//                                            view.updateLine(dataList, 0);
//                                            saveKLineDataToSP();//存入K线数据
//                                            firstLoadHistory = 2;
//                                        }
//
//                                    }
//
//
//                                }
//
//                            }
//
//
//                        }
//                    });
//                }
//
//
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }

    /***
     * dataList转化为K线的数据类型，存入K线的数据
     */
    private void saveKLineDataToSP() {
//        try {
//            JSONArray jsonArray = new JSONArray();
//            JSONObject jsonObject = new JSONObject();
//            JSONObject tmpObj = null;
//            if (dataList != null && dataList.size() > 0) {
//                int count = dataList.size();
//                for (int i = 0; i < count; i++) {
//                    tmpObj = new JSONObject();
//                    tmpObj.put("change", dataList.get(i).getChange());
//                    tmpObj.put("changePercent", dataList.get(i).getChangePercent());
//                    tmpObj.put("close", dataList.get(i).getClose());
//                    tmpObj.put("closeDate", dataList.get(i).getCloseDate());
//                    tmpObj.put("high", dataList.get(i).getHigh());
//                    tmpObj.put("low", dataList.get(i).getLow());
//                    tmpObj.put("open", dataList.get(i).getOpen());
//                    tmpObj.put("openDate", dataList.get(i).getOpenDate());
//                    tmpObj.put("previousClose", dataList.get(i).getPreviousClose());
//                    tmpObj.put("previousCloseDate", dataList.get(i).getPreviousCloseDate());
//                    tmpObj.put("symbol", dataList.get(i).getSymbol());
//                    tmpObj.put("volume", dataList.get(i).getVolume());
//                    jsonArray.put(tmpObj);
//                    tmpObj = null;
//                }
//
//                jsonObject.put("status", "SUCCESS");
//                jsonObject.put("data", jsonArray);
//                quoteObject = jsonObject.toString().replaceAll("\\\\", "");
//                Log.e("quote--", ":quoteObject=" + quoteObject);
//                switch (time) {
//                    case "day":
////                        GalaxyMainActivity.userEditor.putString(CommonType.QUOTE_KLINE_DATA_DAY, quoteObject);
////                        GalaxyMainActivity.userEditor.commit();
//                        LocalSp.getInstance(mContext).saveDay(quoteObject);
//                        break;
//                    case "week":
////                        GalaxyMainActivity.userEditor.putString(CommonType.QUOTE_KLINE_DATA_WEEK, quoteObject);
////                        GalaxyMainActivity.userEditor.commit();
//                        LocalSp.getInstance(mContext).saveWeek(quoteObject);
//                        break;
//                    case "month":
////                        GalaxyMainActivity.userEditor.putString(CommonType.QUOTE_KLINE_DATA_MONTH, quoteObject);
////                        GalaxyMainActivity.userEditor.commit();
//                        LocalSp.getInstance(mContext).saveMonth(quoteObject);
//                        break;
//                }
//            }
//        } catch (JSONException e) {
//            e.printStackTrace();
//        }
    }

//    /**
//     * 修改最后一条
//     *
//     * @param quoteStockInfoSnapData snap的数据
//     * @param oldLastKLineDataBean   K线老的数据
//     * @param newLastKLineDataBean   新合成的数据
//     * @param snapTimeStamp          closeDate
//     */
//    private void replaceAllSnapData(QuoteStockInfoSnapData quoteStockInfoSnapData, QuoteKLineBean.DataBean oldLastKLineDataBean, QuoteKLineBean.DataBean newLastKLineDataBean, long snapTimeStamp, double lastClose) {
//        //需要替换的数据
//        double open = oldLastKLineDataBean.getOpen();
//
//        double high = quoteStockInfoSnapData.getData().getHigh();
//        double low = quoteStockInfoSnapData.getData().getLow();
//
//        //取最大值和最小值
//        if (high < oldLastKLineDataBean.getHigh()) {
//            high = oldLastKLineDataBean.getHigh();
//        }
//        if (low > oldLastKLineDataBean.getLow()) {
//            low = oldLastKLineDataBean.getLow();
//        }
//
//        double close = quoteStockInfoSnapData.getData().getPrice();
//
//        double previousClose = lastClose;
//
//        double change = Arithmetic.subForDouble(close, previousClose);
//
//        double changePercent = Arithmetic.mulForDouble(Arithmetic.divForDouble(change, previousClose, 6), 100);
//
//        double volume;
//
//        double snapVolume = quoteStockInfoSnapData.getData().getVolume();
//
//        if (time.equals("day")) {
//            volume = quoteStockInfoSnapData.getData().getVolume();
//        } else {
//            if (oldVolume != 0) {
//                volume = Arithmetic.addForDouble(oldVolume, snapVolume);
//            } else {
//                volume = dataList.get(0).getVolume();
//            }
//        }
//        long closeData = snapTimeStamp;
//
//        //设置新的数据
//        newLastKLineDataBean.setOpen(open);
//        newLastKLineDataBean.setHigh(high);
//        newLastKLineDataBean.setLow(low);
//        newLastKLineDataBean.setClose(close);
//        newLastKLineDataBean.setChange(change);
//        newLastKLineDataBean.setChangePercent(changePercent);
//        newLastKLineDataBean.setVolume(volume);
//        newLastKLineDataBean.setCloseDate(closeData);
//        newLastKLineDataBean.setPreviousClose(previousClose);
//
//        //不需要改变的数据
//        newLastKLineDataBean.setOpenDate(oldLastKLineDataBean.getOpenDate());
//        newLastKLineDataBean.setPreviousCloseDate(oldLastKLineDataBean.getPreviousCloseDate());
//        newLastKLineDataBean.setSymbol(oldLastKLineDataBean.getSymbol());
//
//        dataList.remove(0);
//        dataList.add(0, newLastKLineDataBean);
//
//        view.updateLine(dataList, 0);
//    }

//    /**
//     * 全取snap的数据,直接添加
//     *
//     * @param quoteStockInfoSnapData snap的数据
//     * @param oldLastKLineDataBean   K线老的数据
//     * @param newLastKLineDataBean   新合成的数据
//     * @param snapTimeStamp          closeDate
//     */
//    private void addAllSnapData(QuoteStockInfoSnapData quoteStockInfoSnapData, QuoteKLineBean.DataBean oldLastKLineDataBean, QuoteKLineBean.DataBean newLastKLineDataBean, long snapTimeStamp) {
//
//        //需要替换的数据
//        double open = quoteStockInfoSnapData.getData().getOpen();
//        double high = quoteStockInfoSnapData.getData().getHigh();
//        double low = quoteStockInfoSnapData.getData().getLow();
//        double close = quoteStockInfoSnapData.getData().getPrice();
//        double change = quoteStockInfoSnapData.getData().getChange();
//        double changePercent = quoteStockInfoSnapData.getData().getPercent();
//        double previousClose = quoteStockInfoSnapData.getData().getPrev();
//        double volume = quoteStockInfoSnapData.getData().getVolume();
//        long closeData = snapTimeStamp;
//
//        //设置新的数据
//        newLastKLineDataBean.setOpen(open);
//        newLastKLineDataBean.setHigh(high);
//        newLastKLineDataBean.setLow(low);
//        newLastKLineDataBean.setClose(close);
//        newLastKLineDataBean.setChange(change);
//        newLastKLineDataBean.setChangePercent(changePercent);
//        newLastKLineDataBean.setVolume(volume);
//        newLastKLineDataBean.setCloseDate(closeData);
//        newLastKLineDataBean.setPreviousClose(previousClose);
//
//        //不需要改变的数据
//        newLastKLineDataBean.setOpenDate(oldLastKLineDataBean.getOpenDate());
//        newLastKLineDataBean.setPreviousCloseDate(oldLastKLineDataBean.getPreviousCloseDate());
//        newLastKLineDataBean.setSymbol(oldLastKLineDataBean.getSymbol());
//
//        dataList.add(0, newLastKLineDataBean);
//        view.updateLine(dataList, 0);
//
//    }


//    /**
//     * 刚进来时请求数据
//     * -  此时只加载数据，不画图，等快照判断逻辑走完，再根据情况画图
//     * -  加载成功后，根据K线类型，存入K线的数据，此数据在页面关闭的时候清除
//     *
//     * @param URL            请求地址
//     * @param stockCode      股票代码
//     * @param dateType       时间间隔
//     * @param adjustmentType 报价调整
//     * @param endDate        结束时间
//     * @param timeNumber     时间单位个数
//     * @param date           时间单位 这个和时间间隔是相对应的也就是 Day 对应的 DateUnit 也是day
//     */
//    private void requestFirstData(String URL, String stockCode, final String dateType, String adjustmentType, long endDate, int timeNumber, String date) {
//
//        ArrayList<Object> params = new ArrayList<>();
//        params.add(stockCode);
//        params.add(dateType);
//        params.add(adjustmentType);
//        params.add(endDate);
//        params.add(timeNumber);
//        params.add(date);
//        Gson gson = new Gson();
//
//        if (!StringUtils.isEmpty(URL)) {
//            OkGo.post(URL)
//                    .upString(gson.toJson(params))
//                    .execute(new StringCallback() {
//                        @Override
//                        public void onSuccess(String s, Call call, Response response) {
//                            QuoteKLineBean quoteKLineBean = JsonUtil.parseJsonToBean(s, QuoteKLineBean.class);
//                            if (quoteKLineBean != null) {
//                                String status = quoteKLineBean.getStatus();
//                                if (!StringUtils.isEmpty(status) && status.equals(CommonType.STATUS_TYPE_QUOTE_SUCCESS)) {
//                                    dataList = new ArrayList<>();
//                                    dataList.addAll(quoteKLineBean.getData());
//                                    oldVolume = dataList.get(0).getVolume();
//                                    if (dataList != null && dataList.size() > 0) {
//                                        //存入数据
//                                        switch (dateType) {
//                                            case "DAY":
////                                                GalaxyMainActivity.userEditor.putString(CommonType.QUOTE_KLINE_DATA_DAY, s);
////                                                GalaxyMainActivity.userEditor.commit();
//                                                LocalSp.getInstance(mContext).saveDay(s);
//                                                break;
//                                            case "WEEK":
////                                                GalaxyMainActivity.userEditor.putString(CommonType.QUOTE_KLINE_DATA_WEEK, s);
////                                                GalaxyMainActivity.userEditor.commit();
//                                                LocalSp.getInstance(mContext).saveWeek(s);
//                                                break;
//                                            case "MONTH":
////                                                GalaxyMainActivity.userEditor.putString(CommonType.QUOTE_KLINE_DATA_MONTH, s);
////                                                GalaxyMainActivity.userEditor.commit();
//                                                LocalSp.getInstance(mContext).saveMonth(s);
//                                                break;
//                                        }
//                                    }
//
////                                    if (view != null) {
//////                                        view.updateLine(dataList,0);
////
////                                        AppBaseNetWork.getInstance().isStockOpeningTime(new AppBaseNetWork.StateCallback() {
////                                            @Override
////                                            public void isSuccess(boolean isSuccess) {
////                                                if (!isSuccess) {
////                                                    view.updateLine(dataList, 0);
////
////                                                }
////                                            }
////                                        });
////
////                                    }
//                                }
//                            }
//                        }
//
//                        @Override
//                        public void onError(Call call, Response response, Exception e) {
//                            super.onError(call, response, e);
//                        }
//
//                    });
//        }
//    }

    @Override
    public void start() {

    }

    @Override
    public void onDestroy() {
        saveKLineDataToSP();//存入K线数据
//        EventBus.getDefault().unregister(this);
    }

}
